const ObjectID = require('mongodb').ObjectID;
const async = require('async');
const orderCode = require('yi-order-number');

module.exports = {
    open: false,
    description: '直接购买商品结算价格',
    handler(req, res, next) {
        const doc = req.body;
        req.mongo.collection('goods').findOne({_id: doc.goodsId,storeId:doc.storeId}, (e0, goods) => {
            if (e0 || !goods) {
                return res.reply({code: 10001, message: '商品信息异常请重试'});
            } else {
                let pay = goods.actualPrice * doc.count; //商品金额

                let authInfo = req.context || req.context.authInfo;
                if (!req.context || !req.context.authInfo) {
                    return res.err.unauthorized();
                }
                authInfo = authInfo.authInfo;
                let id = ObjectID(authInfo._id);

                let orderNo = orderCode.getOrderNo();

                let order = {
                    mallId: goods.mallId,
                    storeId: goods.storeId,
                    userId: id,
                    orderNo: orderNo,
                    orderStatus: "待付款",
                    addressId:doc.addressId,
                    userRemark:doc.userRemark
                };
                let orderItem = {
                    orderNo: orderNo,
                    orderType: goods.type,
                    goodsId: goods._id,
                    name: goods.name,
                    model: goods.model,
                    modelEx: goods.modelEx,
                    goodsCode: goods.goodsCode,
                    color: goods.color,
                    measure: goods.measure,
                    salePrice: goods.salePrice,
                    actualPrice: goods.actualPrice,
                    label: goods.label,
                    image: goods.image,
                    quantity: doc.count
                };

                if(doc.couponId){
                    req.mongo.collection('coupon').findOne({_id:doc.couponId,storeId:doc.storeId,userId:id},(e1,r1)=>{
                        if (e1 || !r1) return res.reply({code:20001,message:'优惠券信息异常请重试',data:pay});
                        let faceValue = r1.facevalue;
                        pay = (pay-faceValue) > 0 ? pay-faceValue : 0;         //减去优惠券面值后支付金额

                        orderItem.payPrice = pay;

                        order.couponId = doc.couponId;
                        order.facevalue = r1.facevalue;

                        order.orderPayAmount = pay;

                        async.auto([
                                callback => req.mongo.collection('order').insertOne(order,callback),
                                callback => req.mongo.collection('orderItem').insertOne(orderItem,callback),
                            ],
                            (error, result) => {
                                if(error) return res.err.mongoError(error);
                                return res.reply({code:0,message:'商品结算价格',data:{goods,pay,faceValue}});
                            });
                    });
                }else{

                    orderItem.payPrice = pay;
                    order.orderPayAmount = pay;

                    async.auto([
                            callback => req.mongo.collection('order').insertOne(order,callback),
                            callback => req.mongo.collection('orderItem').insertOne(orderItem,callback),
                        ],
                        (error, result) => {
                            if(error) return res.err.mongoError(error);
                            return res.reply({code:0,message:'商品结算价格',data:{goods,pay}});
                        });
                }


            }
        });
    }
};